﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using ContactManager.Common;

namespace ContactManager
{
    public partial class GroupFrm : AlSkin.AlForm.AlBaseForm
    {

        public GroupFrm()
        {
            InitializeComponent();
        }

        private void GroupFrm_Load(object sender, EventArgs e)
        {
            this.listBoxGroup.Items.Clear();
            string sql = "select * from groups order by g_sort asc";
            DataSet ds = AccessHelper.ExecuteQuery(sql);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Group group = new Group();
                group.Id = dr[0].ToString();
                group.Name = dr[1].ToString();
                group.Sort = Convert.ToInt32(dr[2].ToString());
                this.listBoxGroup.Items.Add(group);
            }

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text.Trim();
            string sort = this.txtSort.Text.Trim();
            if (name.Length == 0)
            {
                MessageBox.Show("请输入分组名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtName.Focus();
                return;
            }
            if (sort.Length == 0)
            {
                MessageBox.Show("请输入排序值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtSort.Focus();
                return;
            }

            string sql = "insert into groups(g_name,g_sort) values(?,?)";
            OleDbParameter[] parameters = { new OleDbParameter("@g_name", name),
                                                new OleDbParameter("@g_sort", sort)
                                              };
            if (AccessHelper.ExecuteNonQuery(sql, parameters) > 0)
            {
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.btnCancel_Click(this, null);
                this.GroupFrm_Load(this, null);
            }
            else
            {
                MessageBox.Show("添加失败,请重试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text.Trim();
            string sort = this.txtSort.Text.Trim();
            if (name.Length == 0)
            {
                MessageBox.Show("请单击列表，选择一个分组", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            

            Group group = (Group)this.listBoxGroup.SelectedItem;
            string updateSql = "update groups set g_name = ?,g_sort = ? where g_id = ?";
            OleDbParameter[] parameters = { new OleDbParameter("@g_name", name),
                                            new OleDbParameter("@g_sort", sort),
                                            new OleDbParameter("@g_id", group.Id)
                                            };
            if (AccessHelper.ExecuteNonQuery(updateSql, parameters) > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.GroupFrm_Load(this, null);
                this.btnCancel_Click(this, null);
            }
            else
            {
                MessageBox.Show("修改失败,请重试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtName_Leave(object sender, EventArgs e)
        {
            string checkNameSql = "select count(*) from groups where g_name ='" + this.txtName.Text.Trim() + "'";
            if (AccessHelper.ExecuteScalar(checkNameSql) > 0)
            {
                MessageBox.Show("对不起， " + this.txtName.Text.Trim() + " 分组已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtName.Focus();
                this.txtName.Clear();
                return;
            }
        }

        private void GroupFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("您确定要关闭吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
            {
                HomeFrm.gf = null;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtName.Clear();
            this.txtSort.Clear();
        }

        private void listBoxGroup_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (this.listBoxGroup.SelectedItem != null)
            {
                Group group = (Group)this.listBoxGroup.SelectedItem;
                this.txtName.Text = group.Name;
                this.txtSort.Text = group.Sort.ToString();
            }

        }

        private void DelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Group group = (Group)this.listBoxGroup.SelectedItem;
            if (group == null)
            {
                MessageBox.Show("请先选择一个分组", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string delSql = "delete from groups where g_id = ?";
            OleDbParameter[] parameters = {new OleDbParameter("@g_id",group.Id)};
            if (MessageBox.Show("确认要删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                if (AccessHelper.ExecuteNonQuery(delSql,parameters) > 0)
                {
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.GroupFrm_Load(this, null);
                }
                else
                {
                    MessageBox.Show("删除失败，请重试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }

    }
}
